package com.ruoyi.repair.repairFeeInquiry.controller;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.activiti.service.IProcessService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.repair.repairApprove.domain.BizVehicleRepairVo;
import com.ruoyi.repair.repairFeeInquiry.domain.BizVehicleRepairFeeInquiryVo;
import com.ruoyi.system.domain.SysUser;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.repair.repairFeeInquiry.domain.BizVehicleRepairFeeInquiry;
import com.ruoyi.repair.repairFeeInquiry.service.IBizVehicleRepairFeeInquiryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 维修询价Controller
 *
 * @author xzshan
 * @date 2020-06-04
 */
@Controller
@RequestMapping("/repair/repairFeeInquiry")
public class BizVehicleRepairFeeInquiryController extends BaseController {
  private String prefix = "repair/repairFeeInquiry";
  @Autowired
  private TaskService taskService;
  @Autowired
  private RuntimeService runtimeService;
  @Autowired
  private IProcessService processService;
  @Autowired
  private IBizVehicleRepairFeeInquiryService bizVehicleRepairFeeInquiryService;

  @RequiresPermissions("repair:repairFeeInquiry:view")
  @GetMapping()
  public String repairFeeInquiry(ModelMap mmap) {
    mmap.put("currentUser", ShiroUtils.getSysUser());
    return prefix + "/repairFeeInquiry";
  }

  /**
   * 查询维修询价列表
   */
  @RequiresPermissions("repair:repairFeeInquiry:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(BizVehicleRepairFeeInquiryVo bizVehicleRepairFeeInquiryVo) {
    if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
      bizVehicleRepairFeeInquiryVo.setCreateBy(ShiroUtils.getLoginName());
    }
    startPage();
    List<BizVehicleRepairFeeInquiryVo> list = bizVehicleRepairFeeInquiryService.selectBizVehicleRepairFeeInquiryList(bizVehicleRepairFeeInquiryVo);
    return getDataTable(list);
  }

  /**
   * 导出维修询价列表
   */
  @RequiresPermissions("repair:repairFeeInquiry:export")
  @Log(title = "维修询价", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(BizVehicleRepairFeeInquiryVo bizVehicleRepairFeeInquiry) {
    List<BizVehicleRepairFeeInquiryVo> list = bizVehicleRepairFeeInquiryService.selectBizVehicleRepairFeeInquiryList(bizVehicleRepairFeeInquiry);
    ExcelUtil<BizVehicleRepairFeeInquiryVo> util = new ExcelUtil<BizVehicleRepairFeeInquiryVo>(BizVehicleRepairFeeInquiryVo.class);
    return util.exportExcel(list, "repairFeeInquiry");
  }

  /**
   * 新增维修询价
   */
  @GetMapping("/add")
  public String add() {
    return prefix + "/add";
  }

  /**
   * 新增保存维修询价
   */
  @RequiresPermissions("repair:repairFeeInquiry:add")
  @Log(title = "维修询价", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(BizVehicleRepairFeeInquiry bizVehicleRepairFeeInquiry) {
    Long userId = ShiroUtils.getUserId();
    if (SysUser.isAdmin(userId)) {
      return error("提交申请失败：不允许管理员提交申请！");
    }
    return toAjax(bizVehicleRepairFeeInquiryService.insertBizVehicleRepairFeeInquiry(bizVehicleRepairFeeInquiry));
  }

  /**
   * 修改维修询价
   */
  @GetMapping("/edit/{id}")
  public String edit(@PathVariable("id") Long id, ModelMap mmap) {
    BizVehicleRepairFeeInquiry bizVehicleRepairFeeInquiry = bizVehicleRepairFeeInquiryService.selectBizVehicleRepairFeeInquiryById(id);
    mmap.put("bizVehicleRepairFeeInquiry", bizVehicleRepairFeeInquiry);
    return prefix + "/edit";
  }

  /**
   * 修改保存维修询价
   */
  @RequiresPermissions("repair:repairFeeInquiry:edit")
  @Log(title = "维修询价", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(BizVehicleRepairFeeInquiry bizVehicleRepairFeeInquiry) {
    return toAjax(bizVehicleRepairFeeInquiryService.updateBizVehicleRepairFeeInquiry(bizVehicleRepairFeeInquiry));
  }

  /**
   * 删除维修询价
   */
  @RequiresPermissions("repair:repairFeeInquiry:remove")
  @Log(title = "维修询价", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {
    return toAjax(bizVehicleRepairFeeInquiryService.deleteBizVehicleRepairFeeInquiryByIds(ids));
  }

  /**
   * 提交申请
   */
  @Log(title = "维修询价申请业务", businessType = BusinessType.UPDATE)
  @PostMapping("/submitApply")
  @ResponseBody
  public AjaxResult submitApply(Long id) {
    BizVehicleRepairFeeInquiry bizVehicleRepairFeeInquiry = bizVehicleRepairFeeInquiryService.selectBizVehicleRepairFeeInquiryById(id);
    String applyUserId = ShiroUtils.getLoginName();
    bizVehicleRepairFeeInquiryService.submitApply(bizVehicleRepairFeeInquiry, applyUserId, "repair_fee_inquiry", new HashMap<>());
    return success();
  }

  @GetMapping("/repairFeeInquiryTodo")
  public String todoView() {
    return prefix + "/repairFeeInquiryTodo";
  }

  /**
   * 我的待办列表
   *
   * @return
   */
  @PostMapping("/taskList")
  @ResponseBody
  public TableDataInfo taskList(BizVehicleRepairFeeInquiryVo bizVehicleRepairFeeInquiryVo) {
    List<BizVehicleRepairFeeInquiryVo> list = bizVehicleRepairFeeInquiryService.findTodoTasks(bizVehicleRepairFeeInquiryVo, ShiroUtils.getLoginName());
    return getDataTable(list);
  }

  /**
   * 加载审批弹窗
   *
   * @param taskId
   * @param mmap
   * @return
   */
  @GetMapping("/showVerifyDialog/{taskId}")
  public String showVerifyDialog(@PathVariable("taskId") String taskId, ModelMap mmap) {
    Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
    String processInstanceId = task.getProcessInstanceId();
    ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
    BizVehicleRepairFeeInquiryVo bizVehicleRepairFeeInquiryVo = bizVehicleRepairFeeInquiryService.selectBizVehicleRepairFeeInquiryById(new Long(processInstance.getBusinessKey()));
    mmap.put("bizVehicleRepairFeeInquiry", bizVehicleRepairFeeInquiryVo);
    mmap.put("taskId", taskId);
    String verifyName = task.getTaskDefinitionKey().substring(0, 1).toUpperCase() + task.getTaskDefinitionKey().substring(1);
    return prefix + "/task" + verifyName;
  }

  /**
   * 自动绑定页面字段
   */
  @ModelAttribute("preloadBizVehicleRepairFeeInquiryVo")
  public BizVehicleRepairFeeInquiryVo preloadBizVehicleRepairFeeInquiryVo(@RequestParam(value = "id", required = false) Long id, HttpSession session) {
    if (id != null) {
      return bizVehicleRepairFeeInquiryService.selectBizVehicleRepairFeeInquiryById(id);
    }
    return new BizVehicleRepairFeeInquiryVo();
  }

  /**
   * 完成任务
   *
   * @return
   */
  @RequestMapping(value = "/complete/{taskId}", method = {RequestMethod.POST, RequestMethod.GET})
  @ResponseBody
  public AjaxResult complete(@PathVariable("taskId") String taskId, @RequestParam(value = "saveEntity", required = false) String saveEntity,
                             @ModelAttribute("preloadBizVehicleRepairFeeInquiryVo") BizVehicleRepairFeeInquiryVo bizVehicleRepairFeeInquiryVo, HttpServletRequest request) {
    boolean saveEntityBoolean = BooleanUtils.toBoolean(saveEntity);
    processService.complete(null, ShiroUtils.getLoginName(), taskId, bizVehicleRepairFeeInquiryVo.getInstanceId(), "repair_fee_inquiry_titile", bizVehicleRepairFeeInquiryVo.getTitle(), "repair", new HashMap<String, Object>(), request);
    if (saveEntityBoolean) {
      bizVehicleRepairFeeInquiryService.updateBizVehicleRepairFeeInquiry(bizVehicleRepairFeeInquiryVo);
    }
    return success("任务已完成");
  }

  @GetMapping("/repairFeeInquiryDone")
  public String doneView() {
    return prefix + "/repairFeeInquiryDone";
  }

  /**
   * 我的已办列表
   *
   * @return
   */
  @PostMapping("/taskDoneList")
  @ResponseBody
  public TableDataInfo taskDoneList(BizVehicleRepairFeeInquiryVo bizVehicleRepairFeeInquiryVo) {
    List<BizVehicleRepairFeeInquiryVo> list = bizVehicleRepairFeeInquiryService.findDoneTasks(bizVehicleRepairFeeInquiryVo, ShiroUtils.getLoginName());
    return getDataTable(list);
  }

}
